package cn.heycm.blog.console.api;

import cn.heycm.blog.api.console.article.AdminArticleApi;
import cn.heycm.blog.api.console.article.vo.AdminArticleVO;
import cn.heycm.blog.common.entity.result.CodeMsg;
import cn.heycm.blog.common.entity.result.Result;
import cn.heycm.blog.common.utils.Assert;
import cn.heycm.blog.common.utils.ConvertHelper;
import cn.heycm.blog.console.domain.article.model.AdminArticle;
import cn.heycm.blog.console.domain.article.service.AdminArticleService;
import cn.heycm.blog.console.infra.convert.ArticleConverter;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author hey
 * @version 1.0
 * @since 2024/9/26 17:09
 */
@RestController
@AllArgsConstructor
public class AdminArticleApiImpl implements AdminArticleApi {

    private final AdminArticleService adminArticleService;

    @Override
    public Result<AdminArticleVO> find(Long articleId) {
        AdminArticle adminArticle = adminArticleService.findAdminArticle(articleId);
        Assert.notNull(adminArticle, CodeMsg.ARTICLE_NOT_EXISTS);
        AdminArticleVO vo = ArticleConverter.INST.toVO(adminArticle.getArticle());
        vo.setHistories(ConvertHelper.list(adminArticle.getHistories(), ArticleConverter.INST::toVO));
        return Result.success(vo);
    }
}
